package com.example.demo.controller;

import com.example.demo.common.Constant;
import com.example.demo.common.Result;
import com.example.demo.common.SessionUtil;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist") //获取个人文章列表
    public List<ArticleInfo> myList(HttpServletRequest request) {
        HttpSession session=request.getSession();
        if(session!=null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null) {
            //用户正常登陆状态
            UserInfo userInfo=(UserInfo) session.getAttribute(Constant.SESSION_USERINFO_KEY);
            return articleService.getMyList(userInfo.getId());
        }
        return null;
    }

    @RequestMapping("/detail") //获取博客正文
    public Object getDetail(Integer aid) {
        if(aid!=null && aid>0) {
            return Result.success(articleService.getDetail(aid));
        }
        return Result.fail(-1,"查询失败");
    }

    @RequestMapping("/detailbyid")
    public Object getDetailById(HttpServletRequest request,Integer aid) {
        if(aid!=null && aid>0) {
            //验证当前用户是不是作者本人
            ArticleInfo articleInfo=articleService.getDetail(aid);
            UserInfo userInfo= SessionUtil.getLoginUser(request);
            if(userInfo!=null && userInfo.getId()==articleInfo.getUid()&&articleInfo!=null) {
                //作者本人登陆
                return Result.success(articleInfo);
            }
        }
        return Result.fail(-1,"查询失败");
    }

    @RequestMapping("/update")
    public int update(HttpServletRequest request,Integer aid, String title, String content) {
        if(request==null) {
            return 0;
        }
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        if(userInfo!=null&&userInfo.getId()>0) {
            return articleService.update(aid,userInfo.getId(),title,content);
        }
        return 0;
    }

    @RequestMapping("/add")
    public int add(HttpServletRequest request, String title, String content) {
        if(request==null) {
            return 0;
        }
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        if(userInfo!=null&&userInfo.getId()>0) {
            return articleService.add(userInfo.getId(),title, content);
        }
        return 0;
    }

    @RequestMapping("/list") //分页功能
    public List<ArticleInfo>getList(Integer pindex,Integer psize) {
        if(pindex==null||psize==null) {
            return null;
        }
        int offset=(pindex-1)*psize;
        return articleService.getList(psize,offset);
    }

    @RequestMapping("/totalpage") //处理分页功能的尾页
    public Integer getTotalCount(Integer psize) {
        if(psize!=null) {
            int count=articleService.getTotalCount();
            int page=(int)Math.ceil(count*1.0/psize);
            return page;
        }
        return null;
    }

}
